greedy math *1500

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    n, s = map(int, input().split())
    p = 1
    while n % 10 == 0:
        n //= 10
        p *= 10
    a = 0
    while sum(map(int, str(n))) > s:
        d = n % 10
        a += (10 - d) * p
        n = (n + 10 - d)
        while n % 10 == 0:
            n //= 10
            p *= 10
    print(a)

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;


typedef long long int ll;
typedef unsigned long long ull;
typedef long double lld;


#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mod 1000000007
#define float double
#define int long long
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int, int>
#define pll pair<ll, ll>
#define vpi vector<pi>
#define vpll vector<pll>
#define sz(x) ((int)(x).size())
#define prec(n) fixed<<setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define nline "\n"
#define debug(x) cout << #x <<" "; print(x); cout << endl;
 
 
void print(ll t) {cout << t;}
void print(string t) {cout << t;}
void print(char t) {cout << t;}
void print(lld t) {cout << t;}
void print(double t) {cout << t;}
void print(ull t) {cout << t;}
 
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(pair <T, V> p) {cout << "{"; print(p.ff); cout << ","; print(p.ss); cout << "}";}
template <class T> void print(vector <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T> void print(set <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T> void print(multiset <T> v) {cout << "[ "; for (T i : v) {print(i); cout << " ";} cout << "]";}
template <class T, class V> void print(map <T, V> v) {cout << "[ "; for (auto i : v) {print(i); cout << " ";} cout << "]";}
 
ll dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
ll dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int N=2e5+5;
 
 
void solve()
{
    string s;
    ll k;
    cin>>s>>k;
    int n=sz(s);
    s.insert(s.begin(),'0');
    // debug(s)
    vll prefix(n+1,0);
    for (int i = 1; i <= n; i++)
    {
        prefix[i]=prefix[i-1]+(s[i]-'0');
    }
    // debug(prefix)

    if(prefix[n]<=k)
    {
        cout<<0<<endl;
        return;
    }
    int idx=0;
    for (int i = n; i >= 1; i--)
    {
        if(prefix[i-1]+1<=k)
        {
            idx=i;
            break;
        }
    }
    string res;
    int carry=0;
    for (int i = n; i >= idx; i--)
    {
        int num=((s[i]-'0')+carry);
        carry=num/10;
        num=num%10;
        int add=(num==0)? 0: 10-num;
        res.push_back((add+'0'));
        if(add!=0)
        carry=1;
    }
    while (res.size()>1 and res.back()=='0')
    {
        res.pop_back();
    }
    reverse(all(res));
    // debug(res)
    cout<<res<<endl;
}
 
 
 
int32_t main(){
              fast
              int t=1;
              cin>>t;
              // int temp = t;
              while(t--)
              {
//   cout<<"Case #"<<temp-t<<": ";
                  solve();
              }
return 0;
}


Comments

Submit
0 Comments
More Questions

1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort
12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets
1691B - Shoe Shuffling
1706A - Another String Minimization Problem
1695B - Circle Game
1702B - Polycarp Writes a String from Memory
1701A - Grass Field